<template>
  <vxe-table-column
    v-bind="$attrs"
    v-on="$listeners"
  />
</template>

<script>
/**
     * @Author      : maolin
     * @Date        : 2021/4/7
     * @Version     : 1.0
     * @Description :
     **/
import {VXETable} from 'vxe-table';
import {cloneJson, parseTime} from "@/utils";
import Vue from 'vue';
import {Input, Select, Option} from "element-ui";
import {formatAmount, formatDemandNum} from "../../../utils/erp";
import {getDemandNum as getClothDemandNum, materialShapeList} from "../../../utils/erp/cloth";
import {getDemandNum as getDeviceDemandNum} from "../../../utils/erp/device";
import {getDemandNum as getRawMaterialDemandNum} from "../../../utils/erp/rawMaterial";
import {purOrderPurchaseSourceType, selOrderBackSourceType} from "../ErpBusinessPage/config/form";
import { base as request } from "@/request";
import { error } from '../../../utils';
Vue.use(Input);
Vue.use(Select);
Vue.use(Option);

const ErpTableNumber = () => import('@/components/erp/table/ErpTableNumber');
const ErpTableRate = () => import('@/components/erp/table/ErpTableRate');
const ErpTableAmount = () => import('@/components/erp/table/ErpTableAmount');
const ErpTableModal = () => import ('@/components/erp/table/ErpTableModal');
const ErpTableModal01 = () => import ('@/components/erp/table/ErpTableModal01');
const ErpTableModal02 = () => import ('@/components/erp/table/ErpTableModal02');
const ErpTableModal03 = () => import ('@/components/erp/table/ErpTableModal03');
const ErpTableModalName = () => import ('@/components/erp/table/ErpTableModalName');
const ErpTablePurOrderExplain = () => import('@/components/erp/table/ErpTablePurOrderExplain');
const ErpTablePurOrderSell = () => import('@/components/erp/table/ErpTablePurOrderSell');
const ErpTableDate = () => import('@/components/erp/table/ErpTableDate');
const ErpTablePurOrderBackPurchase = () => import('@/components/erp/table/ErpTablePurOrderBackPurchase');
const ErpTableSelOrderBackSend = () => import('@/components/erp/table/ErpTableSelOrderBackSend');
const ErpTableCustomerColor = () => import('@/components/erp/table/ErpTableCustomerColor');
const ErpTableSelOrderSendSell = () => import('@/components/erp/table/ErpTableSelOrderSendSell');
const ErpTableInvColor = () => import('@/components/erp/table/ErpTableInvColor');
const ErpTableNewSupplier = () => import('@/components/erp/table/ErpTableNewSupplier');
const ErpTablePddYLSupplier = () => import('@/components/erp/table/ErpTablePddYLSupplier');
const ErpTableNewSupplier01 = () => import('@/components/erp/table/ErpTableNewSupplier01');
const ErpTableInvRealList = () => import('@/components/erp/table/ErpTableInvRealList');
const ErpTableInvRealList1 = () => import('@/components/erp/table/ErpTableInvRealList1');
const ErpTableInvPddRealList = () => import('@/components/erp/table/ErpTableInvPddRealList');


/**
 * 编辑表格中的列[示例](../eg/ErpDetailTable.html)
 */
export default {
  name: "Index",
  components: {
    ErpTableCustomerColor,
    ErpTableSelOrderBackSend,
    ErpTableNumber,
    ErpTableAmount,
    ErpTableModal,
    ErpTableRate,
    ErpTablePurOrderExplain,
    ErpTablePurOrderSell,
    ErpTablePurOrderBackPurchase,
    ErpTableDate,
    ErpTableSelOrderSendSell,
    ErpTableInvColor,
    ErpTableNewSupplier,
    ErpTableNewSupplier01,
    ErpTableModal02,
    ErpTableModal03,
    ErpTableModalName,
    ErpTableInvRealList,
    ErpTableInvRealList1
  },
  props: {
  },
  data() {
    return {};
  },
  computed: {},
  watch: {
  },
  created() {
    this.initRender();
  },
  mounted() {
  },
  methods: {
    /**
     * 改变下拉框选项，若传了表格对象，
     * @private
     * @param e
     * @param {object} row    行
     * @param {object} column 列
     */
    handleChangeSelect (e, row, column) {
      const table = column.params && column.params.table !== undefined ? column.params.table : null;
      if (table) {
        table.updateStatus({row, column});
      }
    },
    /**
     * 改变物料形态
     * @private
     * @param e
     * @param {object} row    行
     * @param {object} column 列
     */
    handleChangeMaterialShape (e, row, column) {
      let materialShape = row[column.property];
      let gweight = "";
      if (materialShapeList[materialShape]) {
        if (materialShapeList[materialShape].id === 'PRODUCT') {
          gweight = row.mgWeightProduct;
        } else if (materialShapeList[materialShape].id === 'GREY_CLOTH') {
          gweight = row.mgWeightBlank;
        }
      }
      row.gweight = gweight;
      const table = column.params && column.params.table !== undefined ? column.params.table : null;
      if (table) {
        table.updateStatus({row, column});
      }
    },
    /**
     * 渲染的额外编辑表格组件
     * @returns {boolean}
     */
    initRender () {
      const _this = this;
      VXETable.renderer.add('ErpNumber', {
        renderEdit (h, cellRender, { row, column }) {
          let precision = cellRender.attrs && cellRender.attrs.precision !== undefined ? cellRender.attrs.precision : 2;
          return [
            <ErpTableNumber v-model={row[column.property]} precision={precision}></ErpTableNumber>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          let precision = cellRender.attrs && cellRender.attrs.precision !== undefined ? cellRender.attrs.precision : 2;
          let data = formatAmount(row[column.property], precision);
          return [
            <span>{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const precision = cellRender.attrs && cellRender.attrs.precision !== undefined ? cellRender.attrs.precision : 2;
          const data = formatAmount(items[$columnIndex], precision);
          return [
            <span>{data}</span>
          ];
        }
      });
      VXETable.renderer.add('ErpTaxRate', {
        renderEdit (h, cellRender, { row, column }) {
          return [
            <ErpTableRate v-model={row[column.property]}></ErpTableRate>
          ];
        },
        renderCell (h, cellRender, {  row, column  }) {
          let data = [
            <span></span>
          ];
          if (row[column.property] == 0) {
            data = [
              <span>不含税</span>
            ];
          }
          if (row[column.property] > 0) {
            data = [
              <span>{row[column.property]}%</span>
            ];
          }
          return data;
        }
      });
      VXETable.renderer.add('ErpAmount', {
        renderEdit (h, cellRender, { row, column }) {
          return [
            <ErpTableAmount v-model={row[column.property]}></ErpTableAmount>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          const data = formatAmount(row[column.property]);
          return [
            <span>{data}</span>
          ];
        },
        renderDefault (h, cellRender, { row, column }) {
          const data = formatAmount(row[column.property]);
          return [
            <span>{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const data = formatAmount(items[$columnIndex]);
          return [
            <span>{data}</span>
          ];
        }
      });
      VXETable.renderer.add('ErpAmount02', {
        renderEdit (h, cellRender, { row, column }) {
          return [
            <ErpTableAmount v-model={row[column.property]}></ErpTableAmount>
          ];
        },
        renderCell (h, cellRender, {  row, column  }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            
            const numKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'num';
            const priceKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'price';

            const num = row[numKey] || 0;
            const priceOriginal = row[priceKey] || 0;
            data = num * priceOriginal;
          }
          data = formatAmount(data);
          return [
            <span>{ data }</span>
          ];
        }
      });
      VXETable.renderer.add('ErpAmount01', {
        renderEdit (h, cellRender, { row, column }) {
          return [
            <ErpTableAmount v-model={row[column.property]}></ErpTableAmount>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            
            const numKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'num';
            const totalPrice = cellRender.attrs && cellRender.attrs.totalPriceKey ? cellRender.attrs.totalPriceKey : 'totalPrice';
            const num = row[numKey] || 0;
            const totalPriceOriginal = row[totalPrice] || 0;
            data = totalPriceOriginal / num;
          }
          data = formatAmount(data);
          return [
            <span>{ Math.round(data) }</span>
          ];
        },
        renderDefault (h, cellRender, { row, column }) {
          const data = formatAmount(row[column.property]);
          return [
            <span>{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const data = formatAmount(items[$columnIndex]);
          return [
            <span>{data}</span>
          ];
        }
      });
      
      VXETable.renderer.add('ErpTotalPriceOriginal', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            const numKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'sellNum';
            const priceKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'priceUnitOriginal';

            const num = row[numKey] || 0;
            const priceOriginal = row[priceKey] || 0;
            data = num * priceOriginal;
          }
          data = formatAmount(data);
          return [
            <span>{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const data = formatAmount(items[$columnIndex]);
          return [
            <span>{data}</span>
          ];
        }
      });

      VXETable.renderer.add('ErpTotalPriceOriginal01', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            const numKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'num';
            const priceKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'price';

            const num = row[numKey] || 0;
            const priceOriginal = row[priceKey] || 0;
            data = num * priceOriginal;
          }
          data = formatAmount(data);
          return [
            <span>{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const data = formatAmount(items[$columnIndex]);
          return [
            <span>{data}</span>
          ];
        }
      });
      
      VXETable.renderer.add('ErpPriceUnit', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            const priceKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'priceUnitOriginal';
            const rateKey = cellRender.attrs && cellRender.attrs.rateKey ? cellRender.attrs.rateKey : 'rate';

            let rate = column.params && column.params.form && column.params.form[rateKey] ? column.params.form[rateKey] : 0;
            let price = row[priceKey] || 0;
            data = rate * price;

          }
          data = formatAmount(data);

          return [
            <span>{data}</span>
          ];
        },
      });

      // 账面件数
      VXETable.renderer.add('ErpCurPkgNum', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = null;
          if (editable) {
              data = row[column.property] || 0
          }
          data = formatAmount(data);
          row[column.property] = data
          return [
            <span>{data}</span>
          ];
        },
      });

      // 账面粒数
      VXETable.renderer.add('ErpCurGrainNum', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = null;
          if (editable) {
              data = row[column.property] || 0
          }
          data = formatAmount(data);
          row[column.property] = data
          return [
            <span>{data}</span>
          ];
        },
      });

      // 账面数量
      VXETable.renderer.add('ErpCurNum', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = null;
          if (editable) {
              data = row[column.property] || 0
          }
          data = formatAmount(data);
          row[column.property] = data
          return [
            <span>{data}</span>
          ];
        },
      });

      // 盈亏件数
      VXETable.renderer.add('ErpCountPkgNum', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = null;
          if (editable) {
            const checkGrainNumKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'checkPkgNum';
            const curPkgNumKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'curPkgNum';
            let checkGrainNum = row[checkGrainNumKey] || 0;
            let curPkgNum = row[curPkgNumKey] || 0;
            data = curPkgNum - checkGrainNum;
          }
          
          data = formatAmount(data);
          row[column.property] = data
          return [
            <span>{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const data = formatAmount(items[$columnIndex]);
          return [
            <span>{data}</span>
          ];
        }
      });

      // 盈亏数量
      VXETable.renderer.add('ErpCountNum', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = null;
          if (editable) {
            const checkGrainNumKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'checkNum';
            const curPkgNumKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'curNum';
            let checkGrainNum = row[checkGrainNumKey] || 0;
            let curPkgNum = row[curPkgNumKey] || 0;
            data = curPkgNum - checkGrainNum;
          }
          
          data = formatAmount(data);
          row[column.property] = data
          return [
            <span>{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const data = formatAmount(items[$columnIndex]);
          return [
            <span>{data}</span>
          ];
        }
      });

      // 盈亏粒数
      VXETable.renderer.add('ErpCountGrainNum', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = null;
          if (editable) {
            const checkGrainNumKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'checkGrainNum';
            const curPkgNumKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'curGrainNum';
            let checkGrainNum = row[checkGrainNumKey] || 0;
            let curPkgNum = row[curPkgNumKey] || 0;
            data = curPkgNum - checkGrainNum;
          }
          // console.log(row,column,row[column.property])
          data = formatAmount(data);
          row[column.property] = data
          return [
            <span >{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const data = formatAmount(items[$columnIndex]);
          return [
            <span >{data}</span>
          ];
        }
      });

      VXETable.renderer.add('ErpTotalPrice', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            const priceKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'priceUnitOriginal';
            const numKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'sellNum';
            const rateKey = cellRender.attrs && cellRender.attrs.rateKey ? cellRender.attrs.rateKey : 'rate';
            let rate = column.params && column.params.form && column.params.form[rateKey] ? column.params.form[rateKey] : 0;
            let price = row[priceKey] || 0;
            let num = row[numKey] || 0;
            data = rate * price * num;
          }
          data = formatAmount(data);
          return [
            <span>{data}</span>
          ];
        },
        renderFooter (h, cellRender, { items, $columnIndex }) {
          const data = formatAmount(items[$columnIndex]);
          return [
            <span>{data}</span>
          ];
        }
      });
      VXETable.renderer.add('ErpMaterial', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          return [
            <ErpTableModal v-model={row[column.property]} extra={extra} form={row}></ErpTableModal>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.materialName}{row.materialSpecs}</span>
            ];
          } else {
            return [
              <span>{row.materialName}</span>
            ];
          }
        }
      });
      VXETable.renderer.add('ErpMaterial03', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          return [
            <ErpTableModal03 v-model={row[column.property]} extra={extra}  form={row}></ErpTableModal03>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.materialName}</span>
            ];
          } else {
            return [
              <span>{row.materialName}</span>
            ];
          }
        }
      });
      VXETable.renderer.add('ErpMateriaCode', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          return [
            <ErpTableModalName v-model={row[column.property]} extra={extra}  form={row}></ErpTableModalName>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          return [<span>{row.materialIdShowText}</span>];
        }
      });
      VXETable.renderer.add('ErpMateriaName', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          return [
            <ErpTableModalName v-model={row[column.property]} extra={extra}  form={row}></ErpTableModalName>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.materialName || row.name}</span>
            ];
          } else {
            return [
              <span>{row.materialName || row.name}</span>
            ];
          }
        }
      });
      VXETable.renderer.add('ErpMaterial01', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          const options = cellRender.attrs ? cellRender.attrs.options : {};
          return [
            <ErpTableModal01 v-model={row[column.property]} extra={extra} options={options} form={row}></ErpTableModal01>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.brandName || row.brandIdShowText}</span>
            ];
          } else {
            return [
              <span>{row.brandName || row.brandIdShowText}</span>
            ];
          }
        }
      });
      VXETable.renderer.add('ErpMaterial02', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          const options = cellRender.attrs ? cellRender.attrs.options : {};
          return [
            <ErpTableModal02 v-model={row[column.property]} extra={extra} options={options} form={row}></ErpTableModal02>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.sourceOrderItemNo}</span>
            ];
          } else {
            return [
              <span>{row.sourceOrderItemNo}</span>
            ];
          }
        }
      });
      
      VXETable.renderer.add('ErpInvRealList', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          const options = cellRender.attrs ? cellRender.attrs.options : {};
          return [
            <ErpTableInvRealList v-model={row[column.property]} extra={extra} options={options} form={row}></ErpTableInvRealList>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.materialCode}</span>
            ];
          } else {
            return [
              <span>{row.materialCode}</span>
            ];
          }
        }
      });

      VXETable.renderer.add('ErpInvRealList1', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          let fromStockId = column.params.form.fromStockId;
          return [
            <ErpTableInvRealList1 v-model={row[column.property]} fromStockId={fromStockId}  extra={extra} form={row}></ErpTableInvRealList1>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.materialCode}</span>
            ];
          } else {
            return [
              <span>{row.materialCode}</span>
            ];
          }
        }
      });

      VXETable.renderer.add('ErpInvPddReaList', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          const options = cellRender.attrs ? cellRender.attrs.options : {};
          return [
            <ErpTableInvPddRealList v-model={row[column.property]} extra={extra} options={options} form={row}></ErpTableInvPddRealList>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.materialName}</span>
            ];
          } else {
            return [
              <span>{row.materialName}</span>
            ];
          }
        }
      });

      VXETable.renderer.add('ErpNewSupplier', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          return [
            <ErpTableNewSupplier v-model={row[column.property]} extra={extra} form={row}></ErpTableNewSupplier>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.supUserId) {
            return [
              <span>{row.supUserIdShowText}</span>
            ];
          } else {
            return [
              <span>{row.supUserIdShowText}</span>
            ];
          }
        }
      });

      VXETable.renderer.add('ErpPddYLSupplier', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          return [
            <ErpTablePddYLSupplier v-model={row[column.property]} extra={extra} form={row}></ErpTablePddYLSupplier>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.supUserId) {
            return [
              <span>{row.supUserIdShowText}</span>
            ];
          } else {
            return [
              <span>{row.supUserIdShowText}</span>
            ];
          }
        }
      });

      VXETable.renderer.add('ErpNewSupplier01', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          const options = cellRender.attrs ? cellRender.attrs.options : {};
          return [
            <ErpTableNewSupplier01 v-model={row[column.property]} extra={extra} options={options} form={row}></ErpTableNewSupplier01>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.supUserId) {
            return [
              <span>{row.supUserName}</span>
            ];
          } else {
            return [
              <span>{row.supUserName}</span>
            ];
          }
        }
      });

      VXETable.renderer.add('ErpInvColor', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs ? cellRender.attrs.extra : {};
          return [
            <ErpTableInvColor v-model={row[column.property]} extra={extra} form={row}></ErpTableInvColor>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          if (row.materialSpecs) {
            return [
              <span>{row.bizColorCode}</span>
            ];
          } else {
            return [
              <span>{row.bizColorCode}</span>
            ];
          }
        }
      });
      VXETable.renderer.add('ErpPurOrderSource', {
        renderEdit (h, cellRender, { row, column, $rowIndex }) {
          const extra = cellRender.attrs && cellRender.attrs.extra ? cellRender.attrs.extra : {};
          const table = column.params.table;
          const form = column.params.form;
          const sourceType = form.sourceType;
          if (purOrderPurchaseSourceType[sourceType] && purOrderPurchaseSourceType[sourceType].name === 'EXPLAIN_PURCHASE_ORDER') {
            return [
              <ErpTablePurOrderExplain
                v-model={row[column.property]}
                extra={extra}
                form={row}
                field={column.property}
                on-rows-insert={(e) => _this.handlePurRowsInsert(e, row, $rowIndex, table)}
                on-form-change={(e) => _this.handlePurFormChange(e, row, table)}
              ></ErpTablePurOrderExplain>
            ];
          } else if (purOrderPurchaseSourceType[sourceType] && purOrderPurchaseSourceType[sourceType].name === 'SELL_ORDER') {
            return [
              <ErpTablePurOrderSell
                v-model={row[column.property]}
                extra={extra}
                form={row}
                field={column.property}
                on-rows-insert={(e) => _this.handlePurRowsInsert(e, row, $rowIndex, table)}
                on-form-change={(e) => _this.handlePurFormChange(e, row, table)}
              ></ErpTablePurOrderSell>
            ];
          }
        }
      });
      VXETable.renderer.add('ErpSelOrderBackSource', {
        renderEdit (h, cellRender, { row, column, $rowIndex }) {
          const extra = cellRender.attrs && cellRender.attrs.extra ? cellRender.attrs.extra : {};
          const table = column.params.table;
          return [
            <ErpTableSelOrderBackSend
              v-model={row[column.property]}
              extra={extra}
              form={row}
              field={column.property}
              on-rows-insert={(e) => _this.handlePurRowsInsert(e, row, $rowIndex, table)}
              on-form-change={(e) => _this.handlePurFormChange(e, row, table)}
            ></ErpTableSelOrderBackSend>
          ];
        }
      });
      VXETable.renderer.add('ErpSelOrderSendSource', {
        renderEdit (h, cellRender, { row, column, $rowIndex }) {
          const extra = cellRender.attrs && cellRender.attrs.extra ? cellRender.attrs.extra : {};
          const table = column.params.table;
          return [
            <ErpTableSelOrderSendSell
              v-model={row[column.property]}
              extra={extra}
              form={row}
              field={column.property}
              on-rows-insert={(e) => _this.handlePurRowsInsert(e, row, $rowIndex, table)}
              on-form-change={(e) => _this.handlePurFormChange(e, row, table)}
            ></ErpTableSelOrderSendSell>
          ];
        }
      });
      VXETable.renderer.add('ErpCustomerColor', {
        renderEdit (h, cellRender, { row, column }) {
          const extra = cellRender.attrs && cellRender.attrs.extra ? cellRender.attrs.extra : {};
          return [
            <ErpTableCustomerColor
              v-model={row[column.property]}
              extra={extra}
              form={row}
              field={column.property}
            ></ErpTableCustomerColor>
          ];
        }
      });
      VXETable.renderer.add('ErpPurOrderBackSource', {
        renderEdit (h, cellRender, { row, column, $rowIndex }) {
          const extra = cellRender.attrs && cellRender.attrs.extra ? cellRender.attrs.extra : {};
          const table = column.params.table;
          return [
            <ErpTablePurOrderBackPurchase
              v-model={row[column.property]}
              extra={extra}
              form={row}
              field={column.property}
              on-rows-insert={(e) => _this.handlePurRowsInsert(e, row, $rowIndex, table)}
              on-form-change={(e) => _this.handlePurFormChange(e, row, table)}
            ></ErpTablePurOrderBackPurchase>
          ];
        }
      });
      VXETable.renderer.add('ErpInput', {
        renderEdit (h, cellRender, { row, column }) {
          return [
            <el-input v-model={row[column.property]} clearable type="text" placeholder={column.title}></el-input>
          ];
        }
      });
      VXETable.renderer.add('ErpDate', {
        renderEdit (h, cellRender, { row, column }) {
          const valueFormat = 'timestamp';
          const type = 'date';
          return [
            <ErpTableDate
              v-model={row[column.property]}
              clearable
              value-format={valueFormat}
              type={type}
              placeholder={column.title}
            ></ErpTableDate>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          const data = parseTime(row[column.property], '{y}-{m}-{d}');
          return [
            <span>{data}</span>
          ];
        }
      });
      VXETable.renderer.add('ErpSelect', {
        renderEdit (h, cellRender, { row, column }) {
          const valueKey = cellRender.attrs && cellRender.attrs.valueKey ? cellRender.attrs.valueKey : 'value';
          const textKey = cellRender.attrs && cellRender.attrs.textKey ? cellRender.attrs.textKey : 'text';
          const options = _this.getSelectOption(column, valueKey);
          let className = 'el-popper';
          return [
            <el-select
              class={className}
              v-model={row[column.property]}
              placeholder={column.title}
              clearable
              filterable
              on-change={(e) => _this.handleChangeSelect(e,row, column)}
            >
              {options.map(item => {
                return <el-option
                  key={item[valueKey]}
                  label={item[textKey]}
                  value={item[valueKey]}
                >
                </el-option>;
              })}

            </el-select>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          const valueKey = cellRender.attrs && cellRender.attrs.valueKey ? cellRender.attrs.valueKey : 'value';
          const textKey = cellRender.attrs && cellRender.attrs.textKey ? cellRender.attrs.textKey : 'text';
          const options = _this.getSelectOption(column, valueKey);
          let showText = '';
          if (options) {
            for (let i in options) {
              if (options[i][valueKey] == row[column.property]) {
                showText = options[i][textKey];
                break;
              }
            }
          }
          return [
            <span>{showText}</span>
          ];
        }
      });
      VXETable.renderer.add('ErpMaterialShape', {
        renderEdit (h, cellRender, { row, column }) {
          const valueKey = cellRender.attrs && cellRender.attrs.valueKey ? cellRender.attrs.valueKey : 'value';
          const textKey = cellRender.attrs && cellRender.attrs.textKey ? cellRender.attrs.textKey : 'text';
          const options = _this.getSelectOption(column, valueKey);
          return [
            <el-select
              v-model={row[column.property]}
              placeholder={column.title}
              clearable
              filterable
              on-change={(e) => _this.handleChangeMaterialShape(e,row, column)}
            >
              {options.map(item => {
                return <el-option
                  key={item[valueKey]}
                  label={item[textKey]}
                  value={item[valueKey]}
                >
                </el-option>;
              })}

            </el-select>
          ];
        },
        renderCell (h, cellRender, { row, column }) {
          const valueKey = cellRender.attrs && cellRender.attrs.valueKey ? cellRender.attrs.valueKey : 'value';
          const textKey = cellRender.attrs && cellRender.attrs.textKey ? cellRender.attrs.textKey : 'text';
          const options = _this.getSelectOption(column, valueKey);
          let showText = '';
          if (options) {
            for (let i in options) {
              if (options[i][valueKey] == row[column.property]) {
                showText = options[i][textKey];
                break;
              }
            }
          }
          return [
            <span>{showText}</span>
          ];
        }
      });
      VXETable.renderer.add('ErpDemandNum', {
        renderDefault (h, cellRender, { row, column }) {
          function getCellAttr(cellRender, field, defaultValue = null) {
            return cellRender.attrs && cellRender.attrs[field] ?  cellRender.attrs[field] : defaultValue;
          }
          const type = getCellAttr(cellRender, 'type', 'cloth');
          const detailTableDataSource = column.params && column.params.dataSource ? column.params.dataSource : {};
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          const unitListKey = getCellAttr(cellRender, 'unitListKey', 'unitList');
          const sellUnitKey = getCellAttr(cellRender, 'sellUnitKey', 'sellUnit');
          const sellNumKey = getCellAttr(cellRender, 'sellNumKey', 'sellNum');
          const mainUnitKey = getCellAttr(cellRender, 'mainUnitKey', 'mainUnit');
          const doorWidthKey = getCellAttr(cellRender, 'doorWidthKey', 'doorWidth');
          const gweightKey = getCellAttr(cellRender, 'gweightKey', 'gweight');
          const materialShapeKey = getCellAttr(cellRender, 'materialShapeKey', 'materialShape');
          const calMethodKey = getCellAttr(cellRender, 'calMethodKey', 'calMethod');
          // 剩余数量
          const surplusKey = getCellAttr(cellRender, 'surplusKey', 'surplus');
          const unitList = row[unitListKey];
          const sellUnit = row[sellUnitKey];
          const sellNum = row[sellNumKey];
          const mainUnit = row[mainUnitKey];
          const doorWidth = row[doorWidthKey];
          const gweight = row[gweightKey];
          const materialShape = row[materialShapeKey];
          const calMethod = row[calMethodKey];
          const surplus = row[surplusKey];
          let data = null;
          if (type === 'cloth') {
            data = formatDemandNum({row, editable, detailTableDataSource}, (row) => {
              return getClothDemandNum({sellNum, sellUnit, mainUnit, doorWidth, gweight, materialShape, computedMode: calMethod});
            });
          } else if (type === 'device') {
            data = formatDemandNum({row, editable, detailTableDataSource}, (row) => {
              return getDeviceDemandNum({ unitList, sellUnit, sellNum, mainUnit });
            });
          } else if (type === 'rawMaterial') {
            data = formatDemandNum({row, editable, detailTableDataSource}, (row) => {
              return getRawMaterialDemandNum({ unitList, sellUnit, sellNum, mainUnit });
            });
          }
          let num = parseFloat(data.num || 0);
          let demandNum = num.toFixed(2);
          if (demandNum > surplus) {
            let content = '当前数量：' + formatAmount(demandNum,2) + '，剩余数量：' + formatAmount(surplus, 2) + '，已超出剩余数量';
            return [
              <el-tooltip className="item" effect="dark" content={content}
                placement="top-start">
                <span style="color: red"><i class="el-icon-warning"></i> {data.formatter}</span>
              </el-tooltip>

            ];
          } else {
            let content = '当前数量：' + formatAmount(demandNum,2) + '，剩余数量：' + formatAmount(surplus, 2);
            return [
              <el-tooltip className="item" effect="dark" content={content}
                placement="top-start">
                <span>{data.formatter}</span>
              </el-tooltip>

            ];
          }

        },
      });

      VXETable.renderer.add('ErpNum', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            if(!data) {
              const totalPriceKey = cellRender.attrs && cellRender.attrs.totalPriceKey ? cellRender.attrs.totalPriceKey : 'totalPrice';
              const priceKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'price';
              
              let totalPrice = row[totalPriceKey] || 0;
              let price = row[priceKey] || 0;
              data = totalPrice / price;
            } else {
              data = data || 0
            }
          }
          data = formatAmount(data);
          return [
            <span>{data}</span>
          ];
        },
      });

      VXETable.renderer.add('ErpSubNum', {
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            if(!data) {
              const numKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'num';
              const subRateKey = cellRender.attrs && cellRender.attrs.subRateKey ? cellRender.attrs.subRateKey : 'subRate';
              
              let num = row[numKey] || 0;
              let subRate = row[subRateKey] || 0;
              data = num * subRate;
            } else {
              data = data || 0
            }
          }
          data = formatAmount(data);
          return [
            <span>{data}</span>
          ];
        },
      });

      VXETable.renderer.add('ErptotalPrice', {
        renderEdit (h, cellRender, { row, column }) {
          let precision = cellRender.attrs && cellRender.attrs.precision !== undefined ? cellRender.attrs.precision : 2;
          return [
            <ErpTableNumber v-model={row[column.property]} precision={precision}></ErpTableNumber>
          ];
        },
        // renderCell (h, cellRender, { row, column }) {
        //   let precision = cellRender.attrs && cellRender.attrs.precision !== undefined ? cellRender.attrs.precision : 2;
        //   let data = formatAmount(row[column.property], precision);
        //   return [
        //     <span>{data}</span>
        //   ];
        // },
        // renderFooter (h, cellRender, { items, $columnIndex }) {
        //   const precision = cellRender.attrs && cellRender.attrs.precision !== undefined ? cellRender.attrs.precision : 2;
        //   const data = formatAmount(items[$columnIndex], precision);
        //   return [
        //     <span>{data}</span>
        //   ];
        // },
        renderDefault (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
          if (editable) {
            if(!data) {
              const numKey = cellRender.attrs && cellRender.attrs.numKey ? cellRender.attrs.numKey : 'num';
              const priceKey = cellRender.attrs && cellRender.attrs.priceKey ? cellRender.attrs.priceKey : 'price';
              
              let num = row[numKey] || 0;
              let price = row[priceKey] || 0;
              data = num * price;
            } else {
              data = data || 0
            }
          }
          data = formatAmount(data);
          return [
            <span>{data}</span>
          ];
        },
      });

      VXETable.renderer.add('ErpRate', {
        renderDefault  (h, cellRender, { row, column }) {
          const editable = column.params && column.params.editable !== undefined ? column.params.editable : true;
          let data = row[column.property];
           if(row.subUnitId && row.materialId && row.mainUnitId) {
            let subRateIds = {
              subUnitId:row.subUnitId,
              materialId:row.materialId,
              mainUnitId:row.mainUnitId,
            }
            // let a = null
            try {
              (async () => {
                let a = await request.post("/invOrderInit/invOrderInitSubRate/0", subRateIds)
              })();
            } catch(e) {
              console.log(e)
            }
            // console.log(a)
            
          }
          return [
            <span>{data}</span>
          ];
          
        },
      });

    },
    /**
     * 新行插入
     * @private
     * @param {array} rows 多行记录
     * @param {object} currentRow 当前行
     * @param {number} currentRowIndex 当前行所在索引
     * @param {object} table 表格
     */
    handlePurRowsInsert ({ rows }, currentRow, currentRowIndex, table) {
      // 新行插入
      let insertRows = rows;
      if (table) {
        let index = currentRowIndex;
        const {tableData} = table.getTableData();
        if (tableData[index + 1]) {
          // 如果当前行不是最后一条
          table.insertAt(insertRows, tableData[index + 1]);
        } else {
          // 当前为最后一行
          table.insertAt(insertRows, -1);
        }
      }
    },
    /**
     * 表格表单
     * @private
     * @param form
     * @param row
     * @param table
     */
    handlePurFormChange (form, row, table) {
      let currentRow = cloneJson(row);
      currentRow = Object.assign(currentRow, form || {});
      const rowIndex = table.getRowIndex(row);
      // table.insertAt([currentRow], row)
      // table.remove(row)
    },
    /**
     * 获取具有数据源组件的选项
     * @private
     * @param column
     * @param valueKey
     * @returns {*[]|*}
     */
    getSelectOption (column, valueKey) {
      const dataSource = column.params && column.params.dataSource ? column.params.dataSource : {};
      let field = column.property;
      if (dataSource && dataSource !== undefined && dataSource[field] !== undefined) {
        let a = dataSource[field].map((item) => {
          item[valueKey] = item[valueKey] ? parseInt(item[valueKey]) : item[valueKey];
          return item;
        });
        return a;
      }
      return [];
    },

  },
};
</script>

<style lang="scss" scoped>

</style>
